
$(function(){
    ABCIndex();
    bindpingfen();
    bindyinxiang();
//    $("#brandSelect_Btn").click(function(){
//        var $bp = $("#brandPanel");
//        var $btn = $(this);
//        if($btn.attr("on")){
//            $btn.removeAttr("on");
//            if(BrandList.historyObj ){
//                BrandList.historyObj.clearHistory();
//            }
//            BrandList.clearList();
//            $bp.css("display","none");
//        }else{
//            $btn.attr("on","on");
//            $bp.css("display","block");
//        }
//    });
//    $("#categorySelect_Btn").click(function(){
//        var $btn = $(this);
//        if ($btn.attr("on")) {
//            $btn.removeAttr("on");
//            $("#categoryPanel").css("display","none");
//        }else{
//            categoryTree();
//            $btn.attr("on","on");
//            $("#categoryPanel").css("display","block");
//        }
//    });
//    $("#commditySelect_Btn").click(function(){
//        var $btn = $(this);
//        if ($btn.attr("on")) {
//            $btn.removeAttr("on");
//            $("#commdityMenu").css("display","none");
//        }else{
//            commdityMenu();
//            $btn.attr("on","on");
//            $("#commdityMenu").css("display","block");
//        }
//    });
	
    $("#searchShangp").click(function(){
        var brandSelect= $("#brand_input").val();
        var categorySelect= $("#category_input").val();
        var commditySelect= $("#commodity_input").val();

        brandSelect = encodeURI(encodeURI(brandSelect));
        categorySelect = encodeURI(encodeURI(categorySelect));
        commditySelect = encodeURI(encodeURI(commditySelect));
        var id  = $("#id").val();
		
        window.open("/index.php/Home/Industry/index/id/"+id+"/brand_word/"+brandSelect+"/category_word/"+categorySelect+"/commodity_word/"+commditySelect);


		
		
    });
});


//绑定评分下拉框
function bindpingfen(){
    $("#pingfen").change(function(){
        $("#billContent").load("billList.html");
    });
}
//绑定印象下拉框
function bindyinxiang(){
    $("#yinxiang").change(function(){
        $("#billContent").load("billList.html");
    });
}

//品类树
function categoryTree(){
    $("#categoryTree").empty();
    var brandSelectval = $("#brandSelect").text();   
   
    $.get(
        "/index.php/Home/Industry/searchCategory/",
        {
            brand_word:brandSelectval
        },
        function(data){
            if(data!="null"){
                $("#categoryTree").html(data);
                $("#categoryTree span").click(function(){
                    $("#categorySelect").text($(this).text());
                    $("#categorySelect_Btn").removeAttr("on");
                    $("#categoryPanel").css("display","none");
                });
            }
        }
        );
    
}

//品名菜单
function commdityMenu(){
    $("#commdityMenu").empty();
  //  $("#commdityMenu").append('<ul class="menuitem"><li><a href="#">是大块发生地方块</a></li><li><a href="#">是大块发生地方块</a></li><li><a href="#">是大块发生地方块</a></li><li><a href="#">是大块发生地方块</a></li></ul>');
    var brandSelectval = $("#brandSelect").text();   
    var categorySelectval = $("#categorySelect").text();
    
    $.get(
        "/index.php/Home/Industry/searchCommodity/",
        {
            category_word:categorySelectval,
            brand_wod:brandSelectval
        },
        function(data){
            if(data!="null"){
                $("#commdityMenu").html(data);
                $("#commdityMenu ul li a").click(function(){
                    $("#commditySelect").text($(this).text());
                    $("#commdityMenu").css("display","none");
                    $("#commditySelect_Btn").removeAttr("on");
                });
            }
        }
        );

    
}


//拼音索引
var ABCIndex= function(){
    var currentIndex = null;
    var historySearchBrand = "";
    var $brandList = $("#brandList");
    var historySearch = null;
    var t = this;
    $("#abcIndex span").click(function(){
        var $thisindex = $(this);
        if(!currentIndex){
            $thisindex.addClass("on");
            currentIndex=$thisindex;
        }
        if(!historySearch){
            historySearch = new SearchHistory(t);
        }
        var searchText = $thisindex.attr("index");
        historySearch.addHistory(searchText);
    });
    this.clearCurrentIndex = function(){
        if(currentIndex){
            currentIndex.removeClass("on");
            currentIndex = null;
            if(historySearch){
                historySearch.clearHistory();
            }
        }
    }
}

//历史搜索
var SearchHistory = function(parent){
    var $searchHistory = $("#searchHistory");
    var historySearchBrand = "";
    var temp = this;
    $("#closeHistory").click(function(){
        parent.clearCurrentIndex();
    });
    this.addHistory = function(searchText){
        historySearchBrand += searchText;
        $searchHistory.css("display","block");
        $searchHistory.append("<span index=\""+historySearchBrand+"\"><a href=\"#\">"+historySearchBrand+"</a></span>");
        this.rollback();
        $lastchild =  $("#searchHistory span:last-child");
        $lastchild.click(function(){
            var index = $(this).attr("index");//获得历史点击的节点Index
            historySearchBrand=index;
            var currentHistory = $(this);
            currentHistory.nextAll().remove();
            currentHistory.addClass("on");
                       
            BrandList.loadBrand(temp,historySearchBrand);
        });
        $lastchild.click();
    }
    this.clearHistory = function(){
        $searchHistory.find("span").remove();
        $searchHistory.css("display","none");
        historySearchBrand="";
        BrandList.clearList();
    }
    this.rollback = function(){
        $("#searchHistory span").removeClass("on");
        var $lastchild = $("#searchHistory span:last-child");
        $lastchild.addClass("on");
    }
}


//品牌列表
var BrandList = {
    $brandlist:null,
    loadBrand:function(historyObj,index){
        BrandList.historyObj = historyObj;
        BrandList.$brandlist = BrandList.$brandlist==null ? $("#brandList"):BrandList.$brandlist;
        //        $("#brandList").load("brandList.html","",function(data){
        //            if(data=="null"){
        //                historyObj.rollback();
        //            }
        //            $("#brandList span a").click(function(){
        //                $("#brandSelect").text($(this).text());
        //                historyObj.clearHistory();
        //                $("#brandPanel").css("display","none");
        //                $("#brandSelect_Btn").removeAttr("on");
        //            });
        //        });

        $.get(
            "/index.php/Home/Industry/searchBrand/",
            {
                brand_word:index
            },
            function(data){
                if(data=="null"){
                    historyObj.rollback();
                }
                else{
                    $("#brandList").html(data);
                    $("#brandList span a").click(function(){
                        $("#brandSelect").text($(this).text());
                        historyObj.clearHistory();
                        $("#brandPanel").css("display","none");
                        $("#brandSelect_Btn").removeAttr("on");
                    });
                }			
            }
            );

    },
    clearList:function(){
        if(BrandList.$brandlist)
            BrandList.$brandlist.empty();
    }
}